package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbGoods;
import com.dongdongshop.pojo.seller.TbItemCat;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("good")
public class GoodController {

    @Reference
    private GoodsService goodsService;
    @Reference
    private ItemCatService itemCatService;


    private final Logger logger= LoggerFactory.getLogger(GoodsController.class);

    @RequestMapping("toGood")
    public String toGood(){
        return "admin/goods";
    }

    @RequestMapping("listGood")
    @ResponseBody
    public Result listGood(){
        try {
            //获取登录商家
             TbSeller tbSeller =(TbSeller) SecurityUtils.getSubject().getPrincipal();
            List<TbGoods> listGoods = goodsService.listGoodsBySellerId(tbSeller.getSellerId());
            for (TbGoods listGood : listGoods) {
                TbItemCat dataById1 = itemCatService.getDataById(listGood.getCategory1Id());
                listGood.setCategory1Name(dataById1.getName());//各级i目录对应得名字
                TbItemCat dataById2 = itemCatService.getDataById(listGood.getCategory2Id());
                listGood.setCategory2Name(dataById2.getName());
                TbItemCat dataById3 = itemCatService.getDataById(listGood.getCategory3Id());
                listGood.setCategory3Name(dataById3.getName());
            }
            System.out.println(tbSeller+"11111111111111111111111111111111");
            return Result.response(ResultEnum.OK).setData(listGoods);
        }catch (Exception e){
            logger.info("系统错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //提价审核
    @RequestMapping("updateStatus")
    @ResponseBody
    public Result updateStatus(@RequestParam("ids[]")Long[] ids,String flag){
        System.out.println("进入方法");
        System.out.println("flag"+flag);
       try {
           int i=goodsService.updateStatus(ids,flag);
          if (i>0){
               return Result.response(ResultEnum.OK);
           }else {
               return Result.response(ResultEnum.FAIL);
           }
       }catch (Exception e){
           logger.info("系统错误",e);
           return Result.response(ResultEnum.FAIL);
       }
    }
}
